﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using OgilvyOne.Model;
using OgilvyOne.BLL;

public partial class admin_infomation_article_tag_detail : AdminBasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string layerid = Request["layerid"];
            this.PopedomValidate(layerid,QUERY);
            this.headers.Text = new ColumnBll().LayeridColumnConNaiv(layerid);
            string url = "article_tag_detail.aspx?TAG_ID=" + Request.Params["TAG_ID"] + "&layerid=" + Request.Params["layerid"];
            this.headers.Text += "<img src='../images/subnav_dot.gif' width='9' height='9' />&nbsp;&nbsp;<a href='" + url + "'>标签详细信息</a>&nbsp;&nbsp";
            string TAG_ID = Request.Params["TAG_ID"];
            this.paneltotal.Visible = false;
            if (TAG_ID != null && TAG_ID.Trim() != "")
            {
                CmsArticleTagBll catb = new CmsArticleTagBll();
                CmsArticleTagModel catm = catb.GetTagModel(int.Parse(TAG_ID));
                this.TextBox1.Text = catm.TagName;
                /*
                * 代码修改
                * 修改日期：2012-01-15
                * 修改人员：刘扬
                * 修改原因：该变量仅为当前页面使用，故不需要使用Session进行保存，改为使用ViewState进行保存
                * */
                ViewState["Name"] = catm.TagName;

                if (catm.State == 1)
                {
                    this.RadioButton1.Checked = true;
                }
                else
                {
                    this.RadioButton2.Checked = true;
                }
                this.paneltotal.Visible = true;
                this.TextBox2.Text = catm.ClickNum.ToString();
            }
        }
    }

    //提交按钮
    protected void Button2_Click(object sender, EventArgs e)
    {
        string TAG_ID = Request.Params["TAG_ID"];
        if (TAG_ID == null || TAG_ID.Trim() == "")
        {
            if (JudgmentRepeat() == 0)
            {
                this.PopedomValidate(layerid,ADD);
                int ts = insert();
                string logcontent;
                logcontent = "添加标签--" + this.TextBox1.Text.ToString().Trim();
                this.insertLog(logcontent);
                Response.Write("<script>alert('添加成功!')</script>");
                Response.Redirect("article_tag_list.aspx?layerid=" + Request.Params["layerid"]);
                return;

            }
            else
            {
                Response.Write("<script>alert('名称重复!请重新输入!')</script>");
            }
        }
        else
        {
            if (this.TextBox1.Text.ToString().Trim() == ViewState["Name"].ToString())
            {
                bool ts = update();
                if (ts)
                {
                    this.PopedomValidate(layerid,MOD);
                    string logcontent;
                    logcontent = "标签名称--" + ViewState["Name"].ToString() + "修改为--" + this.TextBox1.Text.ToString().Trim();
                    this.insertLog(logcontent);
                    Response.Write("<script>alert('修改成功!')</script>");
                    Response.Redirect("article_tag_list.aspx?layerid=" + Request.Params["layerid"]);
                    return;
                }
            }
            else
            {
                if (JudgmentRepeat() == 0)
                {
                    bool ts = update();
                    if (ts)
                    {
                        this.PopedomValidate(layerid,MOD);
                        string logcontent;
                        logcontent = "标签名称--" + ViewState["Name"].ToString() + "修改为--" + this.TextBox1.Text.ToString().Trim();
                        this.insertLog(logcontent);
                        Response.Write("<script>alert('修改成功!')</script>");
                        Response.Redirect("article_tag_list.aspx?layerid=" + Request.Params["layerid"]);
                        return;
                    }
                }
                else
                {
                    Response.Write("<script>alert('名称重复!请重新输入!')</script>");
                }
            }
        }
        
    }
    //返回按钮
    protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Redirect("article_tag_list.aspx?layerid=" + Request.Params["layerid"]);
        return;
    }
    //添加方法
    private int insert()
    {
        CmsArticleTagModel catm = new CmsArticleTagModel();
        CmsArticleTagBll catb = new CmsArticleTagBll();
        catm.TagName = this.TextBox1.Text.ToString().Trim();
        if (this.RadioButton1.Checked == true)
        {
            catm.State = 1;
        }
        else
        {
            catm.State = 2;
        }
        catm.UserId = GetSessionUsersModel().Aid;
        int ts = catb.Add(catm);
        return ts;
    }
    //修改方法
    private bool update()
    {
        string TAG_ID = Request.Params["TAG_ID"];
        CmsArticleTagModel catm = new CmsArticleTagModel();
        CmsArticleTagBll catb = new CmsArticleTagBll();
        catm.TagId = int.Parse(TAG_ID.Trim());
        catm.TagName = this.TextBox1.Text.ToString().Trim();
        if (this.RadioButton1.Checked == true)
        {
            catm.State = 1;
        }
        else
        {
            catm.State = 2;
        }
        catm.ClickNum = int.Parse(this.TextBox2.Text.ToString().Trim());
        bool bo = catb.Update(catm);
        return bo;
    }
    //判断添加名字是否重复
    private int JudgmentRepeat()
    {
        CmsArticleTagModel catm = new CmsArticleTagModel();
        catm.TagName = this.TextBox1.Text.ToString().Trim();
        CmsArticleTagBll catb = new CmsArticleTagBll();
        IList ls = catb.GetTag(catm);
        if (ls.Count==0)
        {
            return 0;
        }
        else
        {
            return 1;
        }
    }
}
